package com.agen

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.agen.ui.theme.AgenBaseProjTheme

class MainActivity : ComponentActivity() {

    private val viewModel: MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AgenBaseProjTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Greeting(viewModel)
                }
            }
        }
    }
}

@Composable
fun Greeting(viewModel: MainViewModel, modifier: Modifier = Modifier) {
    val hotKeys by viewModel.hotKeys.collectAsState()

    viewModel.queryHotKeys()

    Column(
        modifier = Modifier
            .background(color = Color.White)
    ) {
        Button(
            onClick = {
                viewModel.queryHotKeys()
            }
        ) {
            Text(
                text = "刷新"
            )
        }

        hotKeys.forEachIndexed { index, hotKeyItem ->
            Row {
                Text(
                    text = "$index: ${hotKeyItem.name}",
                    modifier = modifier
                )
            }
        }
    }

}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    AgenBaseProjTheme {
        Greeting(MainViewModel())
    }
}